
#include "Library.h"
#include "BookList.h"
#include "UserList.h"
#include "BookBorrowingList.h"
#include <conio.h>
#include <iostream>
using namespace std;

Library::Library()
{
	a.loadUserList();
	b.loadBooks();
	bb.loadBorrowBook();
}

char* Library::login()
{
	char username[100];
	string pass ="";
	char *password;
	//cin.get();
	cout << "Username: ";
	cin.getline(username,sizeof(username));
	cout << "Password: \n";

	//Change password character to "*"
	char ch;
	ch = _getch();
	while(ch != 13){//character 13 is enter
		if (ch == 8)
		{
			pass.push_back(ch);
			cout << ch;
			ch = 0;
			cout << ch;
			ch = 8;
			cout << ch;
			ch = _getch();
		}
		else
		{
			pass.push_back(ch);
			cout << '*';
			ch = _getch();
		}
	}
	cout << endl;
	password = &pass[0];
	this->EncryptPassword(password);
	
	//a.loadUserList();
	if(a.checkAccount(username,password) != NULL)
	{
		if(strcmp(a.checkAccount(username,password),"3")==0)
		{
			cout <<"Your account has been deleted!"<<endl;
			return "haha";
		}
		return a.checkAccount(username,password);
	}

	cout << "Wrong username or password\n";
	return "haha";
	
}

void Library::beAdmin(char *username)
{
	a.welcome(username);
	int choice;
	char str[1000];
	do 
	{
		cout << "You want to:\n"
			<< "\t1. Manage all users\n"
			<< "\t2. Change your password\n"
			<< "\t3. Update your information\n"
			<< "\t4. Create administrator account\n"
			<< "\t5. Create librarian account\n"
			<< "\t6. Log out\n";

		do 
		{
			cout << "\tChoose: ";
			cin.getline(str,sizeof(str));
			
			if (a.isNumber(str))
				choice = a.convertStringtoNum(str);
			else
				choice = 0;
			
			switch (choice)
			{
			case 1:
				int choice2;
				do 
				{
					cout << "\t\tThe list of user maybe too large\n"
						<< "\t\tYou want to filter it by:\n"
						<< "\t\t1. User Types\n"
						<< "\t\t2. Account Status(on/off)\n"
						<< "\t\t3. Username\n"
						<< "\t\t4. Name of the users\n"
						<< "\t\t5. Don't want to filter, show all\n"
						<< "\t\t6. Back\n";
					do 
					{
						cout << "\t\tChoose: ";
						cin.getline(str,sizeof(str));

						if (a.isNumber(str))
							choice2 = a.convertStringtoNum(str);
						else
							choice2 = 0;

						bool b = false;
						int flag;//Save the type of filter
						//flag: 1 = filterbyAdmin; 2 = filterbyLib ; 3: filterbyRea;
						// 4 = byAccstatus ; 5 = username ; 6 = name of user ; 7 = showall
						int c;
						switch (choice2)
						{
						case 1:
							
							cout << "\t\t\t1. Filter by UserType Administrators\n"
								<< "\t\t\t2. Filter by UserType Librarian\n"
								<< "\t\t\t3. Filter by UserType Reader\n"
								<< "\t\t\t4. Show all\n"
								<< "\t\t\t5. Back\n"
								<< "\t\t\t6. Return to main menu\n";
							//check option
							do 
							{
								cout << "\t\t\tChoose: ";
								cin.getline(str,sizeof(str));

								if (a.isNumber(str))
									c = a.convertStringtoNum(str);
								else
									c = 0;
								switch(c)
								{
								case 1:
									b = a.filterbyUserTypeAdmin();
									a.showsublist();
									flag = 1;
									break;
								case 2:
									b = a.filterbyUserTypeLibrarian();
									a.showsublist();
									flag = 2;
									break;
								case 3:
									b = a.filterbyUserTypeReader();
									a.showsublist();
									flag = 3;
									break;
								case 4:
									a.showAllForAdmin();
									flag = 7;
									break;
								case 5:
									choice2 = -1;
									break;
								case 6:
									choice = -3;
									c = -2;
									break;
								default:
									c = 0;
									cout << "\t\t\tInvalid option, you have to choose again\n";
								}
							} while (c == 0);
							break;
						case 2:
							//Filter by Account Status
							b = a.filterbyAccStatus(2);
							a.showsublist();
							flag = 4;
							choice2 = -1;
							break;
						case 3:
							//Filter by Username
							b = a.filterbyUserName();
							a.showsublist();
							flag = 5;
							choice2 = -1;
							break;
						case 4:
							//Filter by Name of User
							b = a.filterbyName();
							a.showsublist();
							flag = 6;
							choice2 = -1;
							break;
						case 5:
							//Show all list
							b = a.showAllForAdmin();
							a.showsublist();
							flag = 7;
							choice2 = -1;
							break;
						case 6:
							choice = -3;
							break;
						default:
							choice2 = 0;
							cout << "\t\tInvalid option, you have to choose again\n";
						}
						if (b != false)
						{
							int c1;
							do 
							{
								cout << "\t\t\t\tSort list: \n"
									<< "\t\t\t\t1. by Name\n"
									<< "\t\t\t\t2. by UserName\n"
									<< "\t\t\t\t3. by Account Status\n"
									<< "\t\t\t\t4. by UserType\n"
									<< "\t\t\t\tModify info: \n"
									<< "\t\t\t\t5. Ban account\n"
									<< "\t\t\t\t6. Unban account\n"
									<< "\t\t\t\t7. Delete account\n"
									<< "\t\t\t\t8. Reset password\n"
									<< "\t\t\t\t9. Export to XML file\n"
									<< "\t\t\t\t10. Export to CSV file\n"
									<< "\t\t\t\t11. Back\n"
									<< "\t\t\t\t12. Return to main menu\n";
								do 
								{
									cout << "\t\t\t\tChoose: ";
									cin.getline(str,sizeof(str));
									if (a.isNumber(str))
										c1 = a.convertStringtoNum(str);
									else
										c1 = 0;
									int num;

									switch (c1)
									{
									case 1:
										cout << "\t\t\t\t\t1. Ascending\n"
											<< "\t\t\t\t\t2. Descending\n";
										do 
										{
											cout << "\t\t\t\t\tChoose: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												c1 = a.convertStringtoNum(str);
											else
												c1 = 0;
											switch(c1)
											{
											case 1:
												a.sortbyNameAscending();
												break;
											case 2:
												a.sortbyNameDescending();
												break;
											default:
												c1 = 0;
												cout << "Invalid input, you have to choose again\n";
											}
										} while (c1 == 0);
										c1 = -1;
										a.showsublist();
										break;
									case 2:
										cout << "\t\t\t\t\t1. Ascending\n"
											<< "\t\t\t\t\t2. Descending\n";
										do 
										{
											cout << "\t\t\t\t\tChoose: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												c1 = a.convertStringtoNum(str);
											else
												c1 = 0;
											switch(c1)
											{
											case 1:
												a.sortbyUserNameAscending();
												break;
											case 2:
												a.sortbyUserNameDescending();
												break;
											default:
												c1 = 0;
												cout << "Invalid input, you have to choose again\n";
											}
										} while (c1 == 0);
										c1 = -1;
										a.showsublist();
										break;
									case 3:
										cout << "\t\t\t\t\t1. Ascending\n"
											<< "\t\t\t\t\t2. Descending\n";
										do 
										{
											cout << "\t\t\t\t\tChoose: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												c1 = a.convertStringtoNum(str);
											else
												c1 = 0;
											switch(c1)
											{
											case 1:
												a.sortbyAccountStatusAscending();
												break;
											case 2:
												a.sortbyAccountStatusDescending();
												break;
											default:
												c1 = 0;
												cout << "Invalid input, you have to choose again\n";
											}
										} while (c1 == 0);
										c1 = -1;
										a.showsublist();
										break;
									case 4:
										cout << "\t\t\t\t\t1. Ascending\n"
											<< "\t\t\t\t\t2. Descending\n";
										do 
										{
											cout << "\t\t\t\t\tChoose: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												c1 = a.convertStringtoNum(str);
											else
												c1 = 0;
											switch(c1)
											{
											case 1:
												a.sortbyUserTypeAscending();
												break;
											case 2:
												a.sortbyUserTypeDescending();
												break;
											default:
												c1 = 0;
												cout << "Invalid input, you have to choose again\n";
											}
										} while (c1 == 0);
										c1 = -1;
										a.showsublist();
										break;
									case 5:
										do 
										{
											cout << "Choose account: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												num = a.convertStringtoNum(str);
											else
												num = 0;
											if(num == 0)
												cout << "Invalid input, you have to choose again\n";
										} while (num == 0);
										a.banAcc(num);
										if (flag == 1)
										{
											b = a.filterbyUserTypeAdmin();
										} else if (flag == 2)
										{
											b = a.filterbyUserTypeLibrarian();
										} else if (flag == 3)
										{
											b = a.filterbyUserTypeReader();
										} else if (flag == 4)
										{
											b = a.filterbyAccStatus(2);
										} else if (flag == 5)
										{
											b = a.filterbyUserName();
										} else if (flag == 6)
										{
											b = a.filterbyName();
										} else if (flag == 7)
										{
											b = a.showAllForAdmin();
										}

										if (b == true)
										{
											c1 = -1;
											a.showsublist();
										}
										else
										{
											choice2 = -1;
										}
										
										break;
									case 6:
										do 
										{
											cout << "Choose account: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												num = a.convertStringtoNum(str);
											else
												num = 0;
											if(num == 0)
												cout << "Invalid input, you have to choose again\n";
										} while (num == 0);
										a.unbanAcc(num);
										if (flag == 1)
										{
											b = a.filterbyUserTypeAdmin();
										} else if (flag == 2)
										{
											b = a.filterbyUserTypeLibrarian();
										} else if (flag == 3)
										{
											b = a.filterbyUserTypeReader();
										} else if (flag == 4)
										{
											b = a.filterbyAccStatus(1);
										} else if (flag == 5)
										{
											b = a.filterbyUserName();
										} else if (flag == 6)
										{
											b = a.filterbyName();
										} else if (flag == 7)
										{
											b = a.showAllForAdmin();
										}

										if (b == true)
										{
											c1 = -1;
											a.showsublist();
										}
										else
										{
											choice2 = -1;
										}
										break;
									case 7:
										do 
										{
											cout << "Choose account: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												num = a.convertStringtoNum(str);
											else
												num = 0;
											if(num == 0)
												cout << "Invalid input, you have to choose again\n";
										} while (num == 0);
										if(a.FindUsername(num) == NULL)
										{
											cout << "Invalid input, you have to choose again\n";
											cout << "Enter to continue...\n";
											cin.get();
										}
										else
										{
											bb.showBorrowingBook(a.FindUsername(num));
											a.deleteAcc(a.FindUsername(num));
										}
										if (flag == 1)
										{
											b = a.filterbyUserTypeAdmin();
										} else if (flag == 2)
										{
											b = a.filterbyUserTypeLibrarian();
										} else if (flag == 3)
										{
											b = a.filterbyUserTypeReader();
										} else if (flag == 4)
										{
											b = a.filterbyAccStatus(1);
										} else if (flag == 5)
										{
											b = a.filterbyUserName();
										} else if (flag == 6)
										{
											b = a.filterbyName();
										} else if (flag == 7)
										{
											b = a.showAllForAdmin();
										}

										if (b == true)
										{
											c1 = -1;
											a.showsublist();
										}
										else
										{
											choice2 = -1;
										}
										break;
									case 8:
										do 
										{
											cout << "Choose account: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												num = a.convertStringtoNum(str);
											else
												num = 0;
											if(num == 0)
												cout << "Invalid input, you have to choose again\n";
										} while (num == 0);
										a.resetPassword(num);
										c1 = -1;
										a.showsublist();
										break;
									case 9:
										a.WriteUserInfoXMLfile();
										c1 = -1;
										break;
									case 10:
										a.WriteUserInfoCSVfile();
										c1 = -1;
										break;
									case 11:
										choice2 = -1;
										break;
									case 12:
										choice = -3;
										break;
									default:
										c1 = 0;
										cout << "\t\t\t\tInvalid input, you have to choose again\n";
									}
								} while (c1 == 0);
							}while (c1 == -1);
						}
					} while (choice2 == 0);
				} while (choice2 == -1);
				break;
			case 2:
				a.changeYourPassword(username);
				choice = -3;
				break;
			case 3:
				cout << "Currently not available :P\n";
				choice = -3;
				break;
			case 4:
				a.addUsertoFile(1);
				choice = -3;
				break;
			case 5:
				a.addUsertoFile(2);
				choice = -3;
				break;
			case 6:
				return;
			default:
				choice = 0;
				cout << "\tInvalid option, you have to choose again\n";
			}
		} while (choice == 0);
	} while (choice == -3);
	
	
	
}

//specify the tasks for librarians
void Library::beLibrarian(char *username)
{
	a.welcome(username);
	int choice;
	char str[1000];
	do 
	{
		cout << "\t1. Manage reader accounts\n"
			 << "\t2. Manage books\n"
			 << "\t3. Activate accounts\n"
			 << "\t4. Add new book\n"
			 << "\t5. Log out\n";

		do 
		{
			cout << "\tChoose: ";
			cin.getline(str,sizeof(str));
			if (a.isNumber(str))
				choice = a.convertStringtoNum(str);
			else
				choice = 0;
			int c;
			switch (choice)
			{
			case 1:
				
				do 
				{
					cout << "\t\tThe list of all reader can be too large\n"
						 << "\t\tYou want to filter it by:\n";
					cout << "\t\t1. Username\n"
						 << "\t\t2. Name of readers\n"
						 << "\t\t3. Account status\n"
						 << "\t\t4. Don't want to filter, show all\n"
						 << "\t\t5. Back(return to main menu)\n";

					int flag;//Use to save the type of filter that lib choose each time
					//flag: 1:filterbyUsername ; 2:filterbyName ; 3:filterbyAccStatus ; 4:showall
					do 
					{
						cout << "\t\tChoose: ";
						cin.getline(str,sizeof(str));
						if (a.isNumber(str))
							c = a.convertStringtoNum(str);
						else
							c = 0;
						bool b = false;
						switch (c)
						{
						case 1:
							b = a.filterbyUserNameforLib();
							flag = 1;
							a.showsublist();
							c = -1;
							break;
						case 2:
							b = a.filterbyNameforLib();
							flag = 2;
							a.showsublist();
							c = -1;
							break;
						case 3:
							b = a.filterbyAccStatus(1);
							flag = 3;
							a.showsublist();
							c = -1;
							break;
						case 4:
							b = a.showAllForLib();
							flag = 4;
							a.showsublist();
							c = -1;
							break;
						case 5:
							choice = -3;
							break;
						default:
							c = 0;
							cout << "Invalid option, you have to choose again\n";
						}
						if (b != false)
						{
							int c1;
							do 
							{
								cout << "\t\t\tSort list: \n"
									<< "\t\t\t1. by Name\n"
									<< "\t\t\t2. by UserName\n"
									<< "\t\t\t3. by Account Status\n"
									<< "\t\t\tModify info: \n"
									<< "\t\t\t4. Ban account\n"
									<< "\t\t\t5. Unban account\n"
									<< "\t\t\t6. Delete account\n"
									<< "\t\t\t7. Back\n"
									<< "\t\t\t8. Return to main menu\n"
									<< "\t\t\tExport to\n"
									<< "\t\t\t9. a XML file\n"
									<< "\t\t\t10. a CSV file\n";
								do 
								{
									cout << "\t\t\tChoose: ";
									cin.getline(str,sizeof(str));
									if (a.isNumber(str))
										c1 = a.convertStringtoNum(str);
									else
										c1 = 0;
									int num;
									switch (c1)
									{
									case 1:
										cout << "\t\t\t1. Ascending\n"
											 << "\t\t\t2. Descending\n";
										do 
										{
											cout << "\t\t\tChoose: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												c1 = a.convertStringtoNum(str);
											else
												c1 = 0;
											switch(c1)
											{
											case 1:
												a.sortbyNameAscending();
												break;
											case 2:
												a.sortbyNameDescending();
												break;
											default:
												c1 = 0;
												cout << "Invalid input, you have to choose again\n";
											}
										} while (c1 == 0);
										c1 = -1;
										a.showsublist();
										break;
									case 2:
										cout << "\t\t\t1. Ascending\n"
											<< "\t\t\t2. Descending\n";
										do 
										{
											cout << "\t\t\tChoose: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												c1 = a.convertStringtoNum(str);
											else
												c1 = 0;
											switch(c1)
											{
											case 1:
												a.sortbyUserNameAscending();
												break;
											case 2:
												a.sortbyUserNameDescending();
												break;
											default:
												c1 = 0;
												cout << "Invalid input, you have to choose again\n";
											}
										} while (c1 == 0);
										c1 = -1;
										a.showsublist();
										break;
									case 3:
										cout << "\t\t\t1. Ascending\n"
											<< "\t\t\t2. Descending\n";
										do 
										{
											cout << "\t\t\tChoose: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												c1 = a.convertStringtoNum(str);
											else
												c1 = 0;
											switch(c1)
											{
											case 1:
												a.sortbyAccountStatusAscending();
												break;
											case 2:
												a.sortbyAccountStatusDescending();
												break;
											default:
												c1 = 0;
												cout << "Invalid input, you have to choose again\n";
											}
										} while (c1 == 0);
										c1 = -1;
										a.showsublist();
										break;
									case 4:
										do 
										{
											cout << "Choose account: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												num = a.convertStringtoNum(str);
											else
												num = 0;
											if(num == 0)
												cout << "Invalid input, you have to choose again\n";
										} while (num == 0);
										a.banAcc(num);
										//Check flag to filter again because the subhead list now is empty
										//filter so that load a new sublist
										if (flag == 1)
										{
											b = a.filterbyUserNameforLib();
										}
										else if (flag == 2)
										{
											b = a.filterbyNameforLib();
										}
										else if (flag == 3)
										{
											b = a.filterbyAccStatusforLib(1);
										}
										else if (flag == 4)
										{
											b = a.showAllForLib();
										}
										if (b == false)
										{
											c = -1; // The filter list from the last time is empty
													// so the program will back to the filter menu
										}
										else
										{
											a.showsublist();
											c1 = -1; // if the list is not empty, continue
										}
										break;
									case 5:
										do 
										{
											cout << "Choose account: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												num = a.convertStringtoNum(str);
											else
												num = 0;
											if(num == 0)
												cout << "Invalid input, you have to choose again\n";
										} while (num == 0);
										a.unbanAcc(num);
										//Check flag to filter again because the subhead list now is empty
										//filter so that load a new sublist
										if (flag == 1)
										{
											b = a.filterbyUserNameforLib();
										}
										else if (flag == 2)
										{
											b = a.filterbyNameforLib();
										}
										else if (flag == 3)
										{
											b = a.filterbyAccStatusforLib(1);
										}
										else if (flag == 4)
										{
											b = a.showAllForLib();
										}
										if (b == false)
										{
											c = -1; // Do the filter again
										}
										else
										{
											a.showsublist();
											c1 = -1; // if the list is not empty, continue
										}
										break;
									case 6:
										do 
										{
											cout << "Choose account: ";
											cin.getline(str,sizeof(str));
											if (a.isNumber(str))
												num = a.convertStringtoNum(str);
											else
												num = 0;
											if(num == 0)
												cout << "Invalid input, you have to choose again\n";
										} while (num == 0);
										if(strcmp(a.FindUsername(num), "0")==0)
										{
											cout << "Invalid input, you have to choose again\n";
											cout << "Enter to continue...\n";
											cin.get();
										}
										else
										{
											bb.showBorrowingBook(a.FindUsername(num));
											a.deleteAcc(a.FindUsername(num));
										}
										//Check flag to filter again because the subhead list now is empty
										//filter so that load a new sublist
										if (flag == 1)
										{
											b = a.filterbyUserNameforLib();
										}
										else if (flag == 2)
										{
											b = a.filterbyNameforLib();
										}
										else if (flag == 3)
										{
											b = a.filterbyAccStatusforLib(1);
										}
										else if (flag == 4)
										{
											b = a.showAllForLib();
										}
										if (b == false)
										{
											c = -1; // Do the filter again
										}
										else
										{
											a.showsublist();
											c1 = -1; // if the list is not empty, continue
										}
										break;
									case 7:
										c = -1;// Back to the filter menu
										break;
									case 8:
										c = 2; //set to any number except 0 and -1 so that it will pass the while loop
										choice = -3;// Return to main menu
										break;
									case 9:
										a.WriteUserInfoXMLfile();
										break;
									case 10:
										a.WriteUserInfoCSVfile();
										break;
									default:
										c1 = 0;
										cout << "Invalid input, you have to choose again\n";
									}
								} while (c1 == 0);
							} while (c1 == -1);
						}
					} while (c == 0);
				} while (c == -1);
				choice = -3;
				break;
			case 2:
				do 
				{
					cout << "\t\t1. See the list of books\n"
						 << "\t\t2. Borrow book(s)\n"
						 << "\t\t3. Return book(s)\n"
						 << "\t\t4. Back(return to main menu)\n";

					do 
					{
						cout << "\t\tChoose: ";
						cin.getline(str,sizeof(str));
						if (a.isNumber(str))
							c = a.convertStringtoNum(str);
						else
							c = 0;
						bool desc = false;
						switch (c)
						{
						case 1:
							do 
							{
								cout << "\t\t\tThe list of books can be too large\n"
									 << "\t\t\tYou want to filter it by:\n";
								cout << "\t\t\t1. Name of the book\n"
									 << "\t\t\t2. Type of the book\n"
									 << "\t\t\t3. ISBN\n"
									 << "\t\t\t4. Number of books\n"
									 << "\t\t\t5. Year of publication\n"
									 << "\t\t\t6. Show all\n"
									 << "\t\t\t7. Back\n"
									 << "\t\t\t8. Return to main menu\n";

								do 
								{
									bool bl = false;//check NOT-empty of the list; false = empty, true = not empty
									cout << "\t\t\tChoose: ";
									cin.getline(str,sizeof(str));
									if (a.isNumber(str))
										c = a.convertStringtoNum(str);
									else
										c = 0;

									switch (c)
									{
									case 1:
										bl = b.filterbyName();
										c = -1;
										break;
									case 2:
										bl = b.filterbyType();
										c = -1;
										break;
									case 3:
										bl = b.filterbyISBN();
										c = -1;
										break;
									case 4:
										bl = b.filterbyStock();
										c = -1;
										break;
									case 5:
										bl = b.filterbyYoP();
										c = -1;
										break;
									case 6:
										bl = b.showAllBook();
										c = -1;
										break;
									case 7:
										c = -2;
										break;
									case 8:
										choice = -3;
										break;
									default:
										c = 0;
										cout << "Invalid option, you have to choose again\n";
									}

									if (bl != false)
									{
										do 
										{
											cout << "\t\t\t\tSort the list by:\n"
												 << "\t\t\t\t1. Name of the book\n"
												 << "\t\t\t\t2. Type of the book\n"
												 << "\t\t\t\t3. ISBN\n"
												 << "\t\t\t\t4. Number of books\n"
												 << "\t\t\t\t5. Year of publication\n"
												 << "\t\t\t\tExport to\n"
												 << "\t\t\t\t6. A XML file\n"
												 << "\t\t\t\t7. A CSV file\n"
												 << "\t\t\t\t8. Back\n"
												 << "\t\t\t\t9. Return to main menu\n";

											do 
											{
												cout << "\t\t\t\tChoose: ";
												cin.getline(str,sizeof(str));
												if (a.isNumber(str))
													c = a.convertStringtoNum(str);
												else
													c = 0;
												int c1;
												switch (c)
												{
												case 1:
													cout << "\t\t\t\t\t1. Ascending\n"
														<< "\t\t\t\t\t2. Descending\n";
													do 
													{
														cout << "\t\t\t\t\tChoose: ";
														cin.getline(str,sizeof(str));
														if (a.isNumber(str))
															c1 = a.convertStringtoNum(str);
														else
															c1 = 0;
														switch(c1)
														{
														case 1:
															b.SortbyNameAscending();
															break;
														case 2:
															b.SortbyNameDescending();
															break;
														default:
															c1 = 0;
															cout << "Invalid input, you have to choose again\n";
														}
													}while(c1 == 0);
													cout << "------------------------------------------------\n";
													b.showSubList();
													c = -2;
													break;
												case 2:
													cout << "\t\t\t\t\t1. Ascending\n"
														<< "\t\t\t\t\t2. Descending\n";
													do 
													{
														cout << "\t\t\t\t\tChoose: ";
														cin.getline(str,sizeof(str));
														if (a.isNumber(str))
															c1 = a.convertStringtoNum(str);
														else
															c1 = 0;
														switch(c1)
														{
														case 1:
															b.SortbyTypeAscending();
															break;
														case 2:
															b.SortbyTypeDescending();
															break;
														default:
															c1 = 0;
															cout << "Invalid input, you have to choose again\n";
														}
													}while(c1 == 0);
													cout << "------------------------------------------------\n";
													b.showSubList();
													c = -2;
													break;
												case 3:
													cout << "\t\t\t\t\t1. Ascending\n"
														<< "\t\t\t\t\t2. Descending\n";
													do 
													{
														cout << "\t\t\t\t\tChoose: ";
														cin.getline(str,sizeof(str));
														if (a.isNumber(str))
															c1 = a.convertStringtoNum(str);
														else
															c1 = 0;
														switch(c1)
														{
														case 1:
															b.SortbyISBNAscending();
															break;
														case 2:
															b.SortbyISBNDescending();
															break;
														default:
															c1 = 0;
															cout << "Invalid input, you have to choose again\n";
														}
													}while(c1 == 0);
													cout << "------------------------------------------------\n";
													b.showSubList();
													c = -2;
													break;
												case 4:
													cout << "\t\t\t\t\t1. Ascending\n"
														<< "\t\t\t\t\t2. Descending\n";
													do 
													{
														cout << "\t\t\t\t\tChoose: ";
														cin.getline(str,sizeof(str));
														if (a.isNumber(str))
															c1 = a.convertStringtoNum(str);
														else
															c1 = 0;
														switch(c1)
														{
														case 1:
															b.SortbyStockAscending();
															break;
														case 2:
															b.SortbyStockDescending();
															break;
														default:
															c1 = 0;
															cout << "Invalid input, you have to choose again\n";
														}
													}while(c1 == 0);
													cout << "------------------------------------------------\n";
													b.showSubList();
													c = -2;
													break;
												case 5:
													cout << "\t\t\t\t\t1. Ascending\n"
														<< "\t\t\t\t\t2. Descending\n";
													do 
													{
														cout << "\t\t\t\t\tChoose: ";
														cin.getline(str,sizeof(str));
														if (a.isNumber(str))
															c1 = a.convertStringtoNum(str);
														else
															c1 = 0;
														switch(c1)
														{
														case 1:
															b.SortbyYoPAscending();
															break;
														case 2:
															b.SortbyYoPDescending();
															break;
														default:
															c1 = 0;
															cout << "Invalid input, you have to choose again\n";
														}
													}while(c1 == 0);
													cout << "------------------------------------------------\n";
													b.showSubList();
													c = -2;
													break;
												case 6:
													b.WriteBookInfoXMLfile();
													cout << "------------------------------------------------\n";
													b.showSubList();
													c = -2;
													break;
												case 7:
													b.WriteBookInfoCSVfile();
													cout << "------------------------------------------------\n";
													b.showSubList();
													c = -2;
													break;
												case 8:
													c = -1; //set c = -1 so go straight to the forth while (c == -1)
													break;
												case 9:
													c = -3; //Just define c with a number except 0, -1, -2 so it could get out of the loop
													break;
												default:
													c = 0;
													cout << "Invalid option, you have to choose again\n";
												}
											} while (c == 0);
											
										} while (c == -2);
									}
								} while (c == 0);
							} while (c == -1);
							break;
						case 2:
							bb.borrowBook();
							c = -2;
							break;
						case 3:
							bb.returnBookbyUsername();
							c = -2;
							break;
						case 4:
							choice = -3;
							break;
						default:
							c = 0;
							cout << "Invalid option, you have to choose again\n";
						}
					} while (c == 0);
				} while (c == -2);
				choice = -3;
				break;
			case 3:
				if (a.filterbyAccStatus(0) != false) // check if there is any account that inactivate
				{
					do 
					{
						cout << "1. Activate an account\n"
							<< "2. Activate some accounts\n"
							<< "3. Activate all accounts\n"
							<< "4. Back (return to main menu)\n";
						do 
						{
							cout << "Choose: ";
							cin.getline(str,sizeof(str));
							if (a.isNumber(str))
								c = a.convertStringtoNum(str);
							else
								c = 0;
							switch (c)
							{
							case 1:
								int num;
								do 
								{
									cout << "Choose account: ";
									cin.getline(str,sizeof(str));
									if (a.isNumber(str))
										num = a.convertStringtoNum(str);
									else
										num = 0;
									if(num == 0)	
										cout << "Invalid input, you have to choose again\n";
								} while (num == 0);				
								a.activateAnUser(num);
								c = -1;
								break;
							case 2:
								{
									int num;
									int i =0;
									int *ul = new int[1000];
									cout << "Enter a list of which account you want to activate\n"
										<< "Then enter \"0\"(zero) to end\n";
									cout << "Choose: ";
									do
									{
										cin.getline(str,sizeof(str));
										if (a.isNumber(str))
										{
											num = a.convertStringtoNum(str);
											ul[i++] = num;
										}
										else
										{
											num = 1;
											cout << "Invalid input, the program will automatic skip this\n";
										}
									}while(num != 0);
									a.activateSomeUsers(ul);
									c = -1;
									break;
								}
							case 3:
								a.activateAllUser();
								c = -1;
								break;
							case 4:
								choice = -3;
								break;
							default:
								c = 0;
								cout << "Invalid input, you have to choose again\n";
							}

						} while (c == 0);
					} while (c == -1);
				}
				cout << endl;
				choice = -3;
				break;
			case 4:
				b.insertBook();
				choice = -3;
				break;
			case 5:
				return;
			default:
				choice = 0;
				cout << "Invalid option, you have to choose again\n";
			}
		} while (choice == 0);
	} while (choice == -3);
}

//Specify tasks for a reader
void Library::beReader( char *username )
{
	a.welcome(username);
	a.showUserInfo(username);
	int choice;
	int c;
	int order;
	char str[1000];
	do 
	{
		cout << "1. Search book\n"
			 << "2. See list of borrowing books\n"
			 << "3. See list of returned books\n"
			 << "4. Log out\n";
		cout << "Choose: ";
		cin.getline(str,sizeof(str));
		if (a.isNumber(str))
			choice = a.convertStringtoNum(str);
		else
			choice = 0;
		int c=-5;
		switch (choice)
		{
		case 1:
			do{
				cout << "Search by:\n"
					 << "\t1. Name of the book\n"
					 << "\t2. Type of the book\n"
					 << "\t3. ISBN\n"
					 << "\t4. Year of publication\n"
					 << "\t5. Return to main menu\n";
			
				cout << "Choose: ";
				cin.getline(str,sizeof(str));
				if (a.isNumber(str))
					choice = a.convertStringtoNum(str);
				else
					choice = 0;

				switch (choice)
				{
				case 1:
					b.filterbyName();
					choice = -1;
			        break;
				case 2:
					b.filterbyType();
					choice = -1;
			        break;
				case 3:
					b.filterbyISBN();
					choice = -1;
			        break;
				case 4:
					b.filterbyYoP();
					choice = -1;
			        break;
				case 5:
					choice = -2;
			        break;
				default:
					choice = 0;
					cout << "Invalid option, you have to choose again\n";
				}

				if (choice == -1)
				{
					do 
					{
						cout << "Sort the list by:\n"
						     << "1. Name of the book\n"
							 << "2. Type of the book\n"
							 << "3. ISBN\n"
							 << "4. Number of books\n"
							 << "5. Year of publication\n"
							 << "6. Back\n"
							 << "7. Return to main menu\n";


						cout << "Choose: ";
						cin.getline(str,sizeof(str));
						if (a.isNumber(str))
							choice = a.convertStringtoNum(str);
						else
							choice = 0;

						do
						{
							cout <<"Sort in:\n"
								 <<"\t1. Ascending order\n"
								 <<"\t2. Descending order\n";

							cout << "Choose: ";
							cin.getline(str,sizeof(str));
							if (a.isNumber(str))
								order = a.convertStringtoNum(str);
							else
							{
								order = 0;
								cout << "Invalid option, you have to choose again\n";
							}

							if(order == 1 || order ==2)
							{
								break;
							}
							else
							{
								order = 0;
								cout << "Invalid option, you have to choose again\n";
							}
						}while(1);


						switch (choice)
						{
						case 1:
							if(order == 1)
							{
								b.SortbyNameAscending();
								b.showSubList();
							}
							else
							{
								b.SortbyNameDescending();
								b.showSubList();
							}
							choice = 0;
							c = 0;
							break;
						case 2:
							if(order == 1)
							{
								b.SortbyTypeAscending();
								b.showSubList();
							}
							else
							{
								b.SortbyTypeDescending();
								b.showSubList();
							}
							choice = 0;
							c = 0;
							break;
						case 3:
							if(order == 1)
							{
								b.SortbyISBNAscending();
								b.showSubList();
							}
							else
							{
								b.SortbyISBNDescending();
								b.showSubList();
							}
							choice = 0;
							c = -3;
							break;
						case 4:
							if(order == 1)
							{
								b.SortbyStockAscending();
								b.showSubList();
							}
							else
							{
								b.SortbyStockDescending();
								b.showSubList();
							}
							choice = 0;
							c = 0;
							break;
						case 5:
							if(order == 1)
							{
								b.SortbyYoPAscending();
								b.showSubList();
							}
							else
							{
								b.SortbyYoPDescending();
								b.showSubList();
							}
							choice = 0;
							c = 0;
							break;
						case 6:
							choice = 0;
							c = -3;
							break;
						case 7:
							choice = -2;
							c = -3;
							break;
						default:
							choice = 0;
							c = 0;
							cout << "Invalid option, you have to choose again\n";

						}

					}while(c != -3);
				}
			}while(choice !=-2);
			choice = 0;
			break;
		
		case 2:
			bb.showBorrowingBook(username);
			choice = 0;
			break;
		case 3:
			bb.showReturnedBook(username);
			choice = 0;
			break;
		case 4:
			choice = -3;
			break;
		default:
			choice = 0;
			cout << "Invalid option, you have to choose again\n";
		}
	} while (choice != -3);
}



//encrypt user password
char* Library::EncryptPassword(char* pass)
{
	char key[100];
	strcpy(key,pass);
	for(int j=0; j<strlen(key);++j)
	{
		for(int i=0; i<strlen(key);++i)
		{
			pass[i]=pass[i]^key[j];
		}
	}
	return pass;
}